﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using BeautyTech.WebSite.Areas.Admin.Models.Blogs;
using BeautyTech.Services.Blogs;
using AutoMapper;
using BeautyTech.Core.Domain.Blogs;

namespace BeautyTech.WebSite.Areas.Admin.Controllers
{
    
    public class BlogController : Controller
    {

        #region Fields

        private readonly IBlogService _blogService;

        #endregion

        #region Ctor

        public BlogController(IBlogService blogService)
        {
            this._blogService = blogService;
        }

        #endregion

        //
        // GET: /Admin/Blog/

        public ActionResult Index()
        {
            return View();
        }

        //
        // GET: /Admin/Blog/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /Admin/Blog/Create

        public ActionResult Create()
        {
            var model = new BlogPostModel();
            return View(model);
        }

        //
        // POST: /Admin/Blog/Create

        [HttpPost]
        public ActionResult Create(BlogPostModel model)
        {

            // TODO: Add insert logic here
            if (ModelState.IsValid)
            {
                // Mapping de "IList<Category>" vers le ViewModel "IList<CategoryModel>"
                Mapper.CreateMap<BlogPostModel, BlogPost>();
                var blogPost = Mapper.Map<BlogPostModel, BlogPost>(model);

                blogPost.CreatedOn = DateTime.UtcNow;
                _blogService.InsertBlogPost(blogPost);

                return RedirectToAction("Index");
            }

            //If we got this far, something failed, redisplay form
            return View(model);
        }

        //
        // GET: /Admin/Blog/Edit/5

        public ActionResult Edit(int id)
        {
            return View( _blogService.GetBlogPostById(id) );
        }

        //
        // POST: /Admin/Blog/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Admin/Blog/Delete/5
        public ActionResult Delete(int id)
        {
            return View( _blogService.GetBlogPostById(id) );
        }

        //
        // POST: /Admin/Blog/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
